import 'package:fluro/fluro.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_app/pages/article_list_page.dart';
import 'package:flutter_app/pages/home_page.dart';
import 'package:flutter_app/pages/login_page.dart';
import 'package:flutter_app/pages/not_found_page.dart';
import 'package:flutter_app/pages/shop_car_page.dart';
import 'package:flutter_app/pages/webview_page.dart';
import 'package:flutter_app/routers/router_path.dart';

class RouterUtils {
  static final FluroRouter _router = FluroRouter();

  static initRouter() {
    _router.notFoundHandler = Handler(
        handlerFunc: (BuildContext context, Map<String, List<String>> params) {
      debugPrint('未找到目标页');
      return NotFoundPage();
    });

    _router.define(RouterPath.home,
        handler: Handler(handlerFunc: (context, params) => HomePage()));

    _router.define(RouterPath.login,
        handler: Handler(handlerFunc: (context, params) => LoginPage()));

    ///购物车
    _router.define(RouterPath.shop_car,
        handler: Handler(handlerFunc: (context, params) => ShopCarPage()));

    ///WebView
    _router.define(RouterPath.webview,
        handler: Handler(handlerFunc: (context, params) {
      final String isLocalUrl = params['isLocalUrl']?.first;
      final String title = params['title']?.first;
      final String url = params['url']?.first;
      return WebViewPage(title: title, url: url);
    }));

    ///公众号下的文章列表
    _router.define(RouterPath.wxarticle,
        handler: Handler(handlerFunc: (context, params) {
      final String title = params['title']?.first;
      final String id = params['id']?.first;
      return ArticleListPage(title, id);
    }));
  }

  /// 跳到WebView页
  static void goWebViewPage(BuildContext context, String title, String url) {
    //fluro 不支持传中文,需转换
    push(context,
        '${RouterPath.webview}?title=${Uri.encodeComponent(title)}&url=${Uri.encodeComponent(url)}');
  }

  /// 跳到公众号下的文章列表页
  static void goWXArticleListPage(
      BuildContext context, String title, String id) {
    //fluro 不支持传中文,需转换
    push(context,
        '${RouterPath.wxarticle}?title=${Uri.encodeComponent(title)}&id=${Uri.encodeComponent(id)}');
  }

  ///普通跳转
  static void push(BuildContext context, String path,
      {bool replace = false, bool clearStack = false}) {
    unfocus();
    _router.navigateTo(context, path,
        replace: replace,
        clearStack: clearStack,
        transition: TransitionType.native);
  }

  ///可回调数据跳转
  static void pushResult(
      BuildContext context, String path, Function(Object) function,
      {bool replace = false, bool clearStack = false}) {
    unfocus();
    _router
        .navigateTo(context, path,
            replace: replace,
            clearStack: clearStack,
            transition: TransitionType.native)
        .then((value) => {function(value)});
  }

  /// 返回
  static void goBack(BuildContext context) {
    unfocus();
    Navigator.pop(context);
  }

  /// 带参数返回
  static void goBackWithParams(BuildContext context, Object result) {
    unfocus();
    Navigator.pop<Object>(context, result);
  }

  ///取消页面焦点
  static void unfocus() {
    // 使用下面的方式，会触发不必要的build。
    // FocusScope.of(context).unfocus();
    // https://github.com/flutter/flutter/issues/47128#issuecomment-627551073
    FocusManager.instance.primaryFocus?.unfocus();
  }
}
